# -*- coding:utf-8 -*-
# created on 2017/4/30
# 

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from sympy import cos, sin


# 在等腰梯形ABCD中
class XLIsOscelesTrapezoidUpdate(BaseFunction):
    """
    在等腰梯形ABCD中,BC为底边,\\overrightarrow{AD}=t\\overrightarrow{BC},|\\overrightarrow{AB}|=2,
    |\\overrightarrow{AD}|=2,|\\overrightarrow{CD}|=2,|\\overrightarrow{BC}|=4,
    则向量\\frac{1}{2}\\overrightarrow{AD}与\\overrightarrow{BA}-\\overrightarrow{AD}的夹角
    """

    def solver(self, *args):
        quadra_points = args[0].value
        base_line = sympify(args[1].value)
        v_dytx = BaseVectorIsOscelesTrapezoid(quadra_points)
        line_ab = v_dytx.line_AB_name
        line_bc = v_dytx.line_BC_name
        line_cd = v_dytx.line_CD_name
        line_da = v_dytx.line_DA_name
        base_line1 = str(base_line[0]) + str(base_line[1])
        base_line2 = str(base_line[1]) + str(base_line[0])
        if (base_line1 == line_ab) or (base_line2 == line_ab):
            v_dytx.base_line = line_ab
            v_dytx.up_line = line_cd
        elif (base_line1 == line_bc) or (base_line2 == line_bc):
            v_dytx.base_line = line_bc
            v_dytx.up_line = line_da
        elif (base_line1 == line_cd) or (base_line2 == line_cd):
            v_dytx.base_line = line_cd
            v_dytx.up_line = line_ab
        else:
            v_dytx.base_line = line_da
            v_dytx.up_line = line_bc
        self.output.append(v_dytx)
        return self


# 在平行四边形ABCD中
class XLQuadraUpdate(BaseFunction):
    def solver(self, *args):
        quadra_points = args[0].value
        quadra = BaseVectorQuadra(*quadra_points)
        self.output.append(quadra)
        return self


# 已知菱形ABCD
class XLDiamondLengthUpdate001(BaseFunction):
    def solver(self, *args):
        assert 'vDiamond' in self.known
        known = self.known
        v_lx = known['vDiamond']
        edge_length = args[0].sympify()
        v_lx.length = edge_length
        v_eqs = v_lx.Eqs
        v_eqs.append([edge_length, ">", S.Zero])
        self.output.append(v_lx)
        return self


# 在平行四边形ABCD中
class XLQuadraAxisUpdate(BaseFunction):
    def solver(self, *args):
        p1, p2, p3, p4 = args[0].value
        name = p1 + p2 + p3 + p4
        assert name in self.known
        v_pxsbx = self.search(name)
        assert v_pxsbx
        v_eqs = v_pxsbx.Eqs
        point_a = v_pxsbx.point_A_name
        point_b = v_pxsbx.point_B_name
        point_c = v_pxsbx.point_C_name
        point_d = v_pxsbx.point_D_name
        line_ab = v_pxsbx.line_AB_name
        line_bc = v_pxsbx.line_BC_name
        line_ab_value = sympify('a')
        line_bc_value = sympify('b')
        v_eqs.append([line_ab_value, ">", S.Zero])
        v_eqs.append([line_bc_value, ">", S.Zero])
        angle_a = sympify('theta')
        angle_a_name = point_d + point_a + point_b
        self.steps.append(["", "设%s = %s, %s = %s, ∠%s = %s, 则" % (
            new_latex(line_ab), new_latex(line_ab_value), new_latex(line_bc), new_latex(line_bc_value),
            new_latex(angle_a_name), new_latex(angle_a))])
        self.steps.append(["", "以%s为坐标原点, %s所在直线为x轴, 垂直于%s所在直线为y轴建立坐标系" % (
            new_latex(point_a), new_latex(line_ab), new_latex(line_ab))])

        point_a_axis = BasePoint({"name": point_a, "value": [0, 0]})
        point_b_axis = BasePoint({"name": point_b, "value": [line_ab_value, 0]})
        point_c_axis = BasePoint(
            {"name": point_c, "value": [line_bc_value * cos(angle_a) + line_bc_value, line_bc_value * sin(angle_a)]})
        point_d_axis = BasePoint(
            {"name": point_d, "value": [line_bc_value * cos(angle_a), line_bc_value * sin(angle_a)]})
        self.steps.append(["", "∴ %s, %s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(),
                                                     point_c_axis.printing(), point_d_axis.printing())])
        v_pxsbx.point_A_Axis = point_a_axis
        v_pxsbx.point_B_Axis = point_b_axis
        v_pxsbx.point_C_Axis = point_c_axis
        v_pxsbx.point_D_Axis = point_d_axis
        self.output.append(v_pxsbx)
        self.label.add("建系-平行四边形类")
        return self


# 在等腰梯形ABCD中
class XLIsOscelesTrapezoidAxisUpdate(BaseFunction):
    """
    在等腰梯形ABCD中,BC为底边,\\overrightarrow{AD}=t\\overrightarrow{BC},|\\overrightarrow{AB}|=2,|\\overrightarrow{AD}|=2,
    |\\overrightarrow{CD}|=2,|\\overrightarrow{BC}|=4,则向量\\frac{1}{2}\\overrightarrow{AD}
    与\\overrightarrow{BA}-\\overrightarrow{AD}的夹角
    """

    def solver(self, *args):
        name = args[0].value
        assert name in self.known
        v_dytx = self.search(name)
        assert v_dytx.type == 'vIsOscelesTrapezoid'
        v_eqs = v_dytx.Eqs
        point_a = v_dytx.point_A_name
        point_b = v_dytx.point_B_name
        point_c = v_dytx.point_C_name
        base_line = v_dytx.base_line
        up_line = v_dytx.up_line
        base_line_value = 2 * sympify('a1')
        up_line_value = 2 * sympify('b1')
        isosceles_value = sympify('c1')
        v_eqs.append([base_line_value, ">", S.Zero])
        v_eqs.append([up_line_value, ">", S.Zero])
        v_eqs.append([isosceles_value, ">", S.Zero])
        v_eqs.append([base_line_value, ">", up_line_value])
        self.steps.append(["", "设等腰梯形的下底边长为%s,上底边长为%s,腰为%s,则" % (
            new_latex(base_line_value), new_latex(up_line_value), new_latex(isosceles_value))])
        self.steps.append(["", "以%s为x轴,%s的中垂线为y轴,建立坐标系" % (new_latex(base_line), new_latex(up_line))])
        base_line_left_axis = BasePoint({"name": base_line[0], "value": [- base_line_value / 2, 0]})
        base_line_right_axis = BasePoint({"name": base_line[1], "value": [base_line_value / 2, 0]})
        up_line_left_axis = BasePoint({"name": up_line[0],
                                       "value": [up_line_value / 2,
                                                 sqrt(isosceles_value ** 2 - (
                                                         base_line_value / 2 - up_line_value / 2) ** 2)]})
        up_line_right_axis = BasePoint({"name": up_line[1],
                                        "value": [- up_line_value / 2,
                                                  sqrt(isosceles_value ** 2 - (base_line_value / 2 -
                                                                               up_line_value / 2) ** 2)]})
        point_a_axis = None
        point_b_axis = None
        point_c_axis = None
        point_d_axis = None
        if base_line[0] == point_a:
            point_a_axis = base_line_left_axis
        elif base_line[0] == point_b:
            point_b_axis = base_line_left_axis
        elif base_line[0] == point_c:
            point_c_axis = base_line_left_axis
        else:
            point_d_axis = base_line_left_axis

        if base_line[1] == point_a:
            point_a_axis = base_line_right_axis
        elif base_line[1] == point_b:
            point_b_axis = base_line_right_axis
        elif base_line[1] == point_c:
            point_c_axis = base_line_right_axis
        else:
            point_d_axis = base_line_right_axis

        if up_line[0] == point_a:
            point_a_axis = up_line_left_axis
        elif up_line[0] == point_b:
            point_b_axis = up_line_left_axis
        elif up_line[0] == point_c:
            point_c_axis = up_line_left_axis
        else:
            point_d_axis = up_line_left_axis

        if up_line[1] == point_a:
            point_a_axis = up_line_right_axis
        elif up_line[1] == point_b:
            point_b_axis = up_line_right_axis
        elif up_line[1] == point_c:
            point_c_axis = up_line_right_axis
        else:
            point_d_axis = up_line_right_axis

        v_dytx.point_A_Axis = point_a_axis
        v_dytx.point_B_Axis = point_b_axis
        v_dytx.point_C_Axis = point_c_axis
        v_dytx.point_D_Axis = point_d_axis
        self.output.append(v_dytx)
        self.label.add("建系-等腰梯形类")
        return self
